<?php
namespace Home\Controller;
use Common\Controller\CommonController;
class CityController extends CommonController {
	
	function index(){
		
		$where['city']=I('key');
		
		if($where['city']!=""){
		$count=M('city')->where($where)->count();
		$page=new \Think\Page($count,10);
		$show=$page->show();	
		$re=M('city')->limit($page->firstRow.','.$page->listRows)->where($where)->select();
		foreach ($re as $key => $value ) {
			$id=$value['id'];
			$value['num']=M('Apt')->where('cid='.$id)->count();
			$a[]=$value;	
		}
		$re=$a;
		}else{
			
		$count=M('city')->count();
		$page=new \Think\Page($count,10);
		$show=$page->show();	
		$re=M('city')->limit($page->firstRow.','.$page->listRows)->select();
		foreach ($re as $key => $value ) {
			$id=$value['id'];
			$value['num']=M('Apt')->where('cid='.$id)->count();
			$a[]=$value;	
		}
		$re=$a;		
		}
		$this->assign('page',$show);
		$this->assign('list',$re);
		$this->display('index');
	}
	
	function addcity(){
		
		$city=I('city');
		if($city){
		$data=array('city'=>$city,);
		$re=M('city')->add($data);	
		if($re){
		$this->redirect('City/index');	
		}
		
		}else{
		$this->display('add');
		
		}
	}

	function delcity(){
		$where['id']=I('id');
		$re=M('city')->where($where)->delete();
		if($re){
		$this->redirect('City/index');	
		}
	}
	
	
	
}